package com.bw.zuoye0722

import android.os.Bundle
import android.text.Editable
import android.text.TextWatcher
import android.util.Log
import androidx.activity.enableEdgeToEdge
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import androidx.lifecycle.lifecycleScope
import com.alibaba.android.arouter.facade.annotation.Autowired
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.blankj.utilcode.util.SPUtils
import com.blankj.utilcode.util.ToastUtils
import com.bw.base.mvvm.BaseActivity
import com.bw.zuoye0722.databinding.ActivityCodeBinding
import com.bw.zuoye0722.login.LoginViewModel
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch

@Route(path ="/app/CodeActivity")
class CodeActivity : BaseActivity<ActivityCodeBinding,LoginViewModel>() {
    @JvmField
    @Autowired
    var phone=""
    override fun initView() {
        ARouter.getInstance().inject(this)
        binding.back.setOnClickListener {
            finish()
        }
        binding.tel.setText("已发送至+86  ${phone.replaceRange(3,7,"****")}")
        //倒计时
        lifecycleScope.launch {
            for (i in 60 downTo 1) {
                binding.tvTime.text="重新发送/${i}秒"
                delay(1000)
            }
        }
        binding.et6.addTextChangedListener(object :TextWatcher{
            override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {

            }

            override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {
                if(s!=null){
                    var codeval=binding.et1.text.toString()+binding.et2.text.toString()+binding.et3.text.toString()+binding.et4.text.toString()+binding.et5.text.toString()+binding.et6.text.toString()
                        viewModel.login(codeval,phone)
                }
            }

            override fun afterTextChanged(s: Editable?) {

            }

        })

    }

    override fun initData() {
        viewModel.loginSuccess.observe(this){
            if(it.code==200){
                ARouter.getInstance().build("/app/MainActivity").navigation()
            }
            ToastUtils.showLong(it.message)
        }
    }

    override fun getViewModel(): Class<LoginViewModel> {
        return LoginViewModel::class.java
    }

    override fun getLayoutId(): Int {
       return R.layout.activity_code
    }
}